﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESOL.Common.Entity;
using Suzsoft.Smart.Data;
using ESOL.Business.Prod;

namespace ESOL.Business.ProcessingService
{
    public class WorkCenterService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="workcenter">工作中心实体</param>
        /// <param name="persons">工作中心的人力资源实体集</param>
        /// <param name="assets">工作中心的设备资源实体集</param>
        /// <returns></returns>
        public static bool Insert(ProdWorkcentersEntity workcenter, List<ProdWorkcentersPersonEntity> persons, List<ProdWorkcentersAssetsEntity> assets)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Insert(workcenter, broker);
                    foreach(ProdWorkcentersPersonEntity m in persons)
                    {
                        m.WorkcentersCode = workcenter.Code;
                    }
                    foreach (ProdWorkcentersAssetsEntity m in assets)
                    {
                        m.WorkcentersCode = workcenter.Code;
                    }
                    DataAccess.Insert<ProdWorkcentersPersonEntity>(persons, broker);
                    DataAccess.Insert<ProdWorkcentersAssetsEntity>(assets, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="workcenter"></param>
        /// <param name="addedPersons"></param>
        /// <param name="modifiedPersons"></param>
        /// <param name="delPersons"></param>
        /// <param name="addedAssets"></param>
        /// <param name="modifiedAssets"></param>
        /// <param name="delAssets"></param>
        /// <returns></returns>
        public static bool Update(ProdWorkcentersEntity workcenter,
            List<ProdWorkcentersPersonEntity> addedPersons, List<ProdWorkcentersPersonEntity> modifiedPersons, List<ProdWorkcentersPersonEntity> delPersons,
            List<ProdWorkcentersAssetsEntity> addedAssets, List<ProdWorkcentersAssetsEntity> modifiedAssets, List<ProdWorkcentersAssetsEntity> delAssets)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    foreach (ProdWorkcentersPersonEntity m in addedPersons)
                    {
                        m.WorkcentersCode = workcenter.Code;
                    }
                    foreach (ProdWorkcentersAssetsEntity m in addedAssets)
                    {
                        m.WorkcentersCode = workcenter.Code;
                    }
                    DataAccess.Update(workcenter, broker);
                    DataAccess.DeleteEntities<ProdWorkcentersPersonEntity>(delPersons, broker);
                    DataAccess.Update<ProdWorkcentersPersonEntity>(modifiedPersons, broker);
                    DataAccess.Insert<ProdWorkcentersPersonEntity>(addedPersons, broker);
                    DataAccess.DeleteEntities<ProdWorkcentersAssetsEntity>(delAssets, broker);
                    DataAccess.Update<ProdWorkcentersAssetsEntity>(modifiedAssets, broker);
                    DataAccess.Insert<ProdWorkcentersAssetsEntity>(addedAssets, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

    }
}
